import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 目标:在用户表userinfo中插入1条表记录
 * INSERT INTO userinfo(id,username,password,nickname,age) VALUES(NULL,'Tony','123456','tony',18)
 */
public class JDBCDemo1 {
    public static void main(String[] args) {
        try {
            // 1.加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2.使用数据库连接对象
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/tedu", "root", "root");
            // 3.创建SQL语句对象执行
            Statement statement = connection.createStatement();
            // 4.执行SQL语句
            // executeUpdate(): 执行增删改语句
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入用户名:");
            String username=sc.nextLine();
            System.out.println("请输入密码:");
            String password=sc.nextLine();
            System.out.println("请输入昵称:");
            String nickname=sc.nextLine();
            System.out.println("请输入年龄:");
            int age=sc.nextInt();

            String sql="INSERT INTO userinfo(id,username,password,nickname,age) VALUES(NULL,'"+username+"','"+password+"','"+nickname+"',"+age+")";

            int i = statement.executeUpdate(sql);
            // 5.断开连接
            connection.close();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}
